package edcom.server;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edcom.client.ImageService;
import edcom.client.TaggedImage;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ImageServiceImpl extends RemoteServiceServlet implements ImageService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -667708777358804664L;

	public TaggedImage[] getImagesFromServer(String classid) {

		TaggedImage[] retval = null;

		try {
			String connectionURL = "jdbc:mysql://localhost/edcom";
			Connection connection = null;
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection(connectionURL, "root", 
					"durendal");
			
			if(!connection.isClosed()){
				
				ResultSet rs = null;
				PreparedStatement pstmt = connection.prepareStatement("SELECT" 
						+ " * FROM images WHERE course='" + classid + "'");
				rs = pstmt.executeQuery();
				
				rs.last();
				retval = new TaggedImage[rs.getRow() + 1];
				rs.first();
				
				int ct = 0;
				do {
					retval[ct++] = new TaggedImage(rs.getString(1), 
							rs.getString(2), rs.getString(3), rs.getString(4), 
							rs.getString(5));
				} while (rs.next());
				
				connection.close();
			}
			
		} catch (ClassNotFoundException cnfe) {
//			retval = null; /*
			retval = new TaggedImage[1];
			retval[0] = new TaggedImage("CNFE", "ERROR", "CNFE", "CNFE", cnfe.getMessage()); // */
		} catch (SQLException sqle) {
//			retval = null; /*
			retval = new TaggedImage[1];
			retval[0] = new TaggedImage("SQLE", "ERROR", "SQLE", "SQLE", sqle.getMessage()); // */
		} catch (Exception e) {
//			retval = null; /*
			retval = new TaggedImage[1];
			retval[0] = new TaggedImage("E", "ERROR", "E", "E", e.getMessage()); // */
		}
		/*
		retval = new TaggedImage[1];
		retval[0] = new TaggedImage("E", "ERROR", "E", "E", classid);//*/

		return retval;
	}

	@SuppressWarnings("deprecation")
	public String deleteImage(TaggedImage img, String base) {

		if(img == null) {
			return "ERROR: parameter cannot be null";
		}
		
		if(base == null) {
			base = this.getThreadLocalRequest().getRealPath("/");
		}
		
		base = base.substring(0, base.lastIndexOf("krusnik") + 8);
		
		String filename = base + "images/" + img.getId() + img.getExtension();
		
		File file = new File(filename); 

		try {
			if(file.exists()){
				String connectionURL = "jdbc:mysql://localhost/edcom";
				Connection connection = null;
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				connection = DriverManager.getConnection(connectionURL, "root", 
						"durendal");

				if(!connection.isClosed()){
					PreparedStatement pstmt = connection.prepareStatement(
							"DELETE FROM images WHERE id='" + img.getId() + 
							"' AND course='" + img.getClassName() + "'");
					pstmt.execute();
					connection.close();
				}

				if(file.delete()) 
					return "success"; /*
					return "{" + filename + "}"; //*/
				else return "ERROR: failed to delete \"" + file + "\"";
			} else {
				return "ERROR: file \"" + file + "\" does not exist";
			}
		} catch (ClassNotFoundException cnfe) {
			return "ERROR: " + cnfe.getMessage();
		} catch (SQLException sqle) {
			return "ERROR: " + sqle.getMessage();
		} catch (Exception e) {
			return "ERROR: " + e.getMessage();
		}
	}

}
